﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using ERP.Membership;
using System.Threading;

namespace Entities
{
    public class BaseERPPage : Page
    {
        private UserData _currentUser = null;
        public UserData CurrentUser
        {
            get
            {
                if (_currentUser == null)
                {
                    ERPMembership membership = new ERPMembership();
                    _currentUser = membership.GetCurrentLoggedInUser();
                }
                return _currentUser;
            }
        }

        private IEnumerable<Permission> _requieredPermissions = Enumerable.Empty<Permission>();
        public BaseERPPage(params Permission[] permissions)
        {
            _requieredPermissions = permissions.ToList();
        }
        protected override void InitializeCulture()
        {
            if (Session["culture"] == null)
            {
                Session["culture"] = "he";
            }
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo((string)Session["Culture"]);

            UICulture = (string)Session["culture"];

            base.InitializeCulture();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (CurrentUser == null)
            {
                Response.Redirect("Account/Login.aspx");
            }
            if (!CurrentUser.IsUserAllowed(_requieredPermissions))
            {
                Response.Redirect("NoPrivs.aspx");
            }

            base.OnLoad(e);
        }
    }
}
